﻿'use strict';
var app = angular.module('app', [
        // Angular modules 
        'ngAnimate',        // animations
        'ngRoute',          // routing
        'ngSanitize',       // sanitizes html bindings (ex: sidebar.js)

        // Custom modules 
        //'common',           // common functions, logger, spinner
        //'common.bootstrap', // bootstrap dialog wrapper functions        

        // 3rd Party Modules
        'ui.bootstrap',      // ui-bootstrap (ex: carousel, pagination, dialog)
        'LocalStorageModule'
]);

app.config(function ($routeProvider) {

    $routeProvider.when("/home", {
        controller: "homeController",
        templateUrl: "/app/views/home.html"
    });

    $routeProvider.when("/login", {
        controller: "loginController",
        templateUrl: "/app/views/login.html"
    });

    $routeProvider.when("/signup", {
        controller: "signupController",
        templateUrl: "/app/views/signup.html"
    });

    $routeProvider.when("/notauthorized", {
        controller: "notauthorizedController",
        templateUrl: "/app/views/notauthorized.html"
    });

    $routeProvider.when("/student", {
        //controller: "studentController",
        templateUrl: "app/views/student.html"
    });
    $routeProvider.when("/profesor", {
        //controller: "profesorController",
        templateUrl: "app/views/profesor.html"
    });
    $routeProvider.when("/nota", {
        //controller: "notaController",
        templateUrl: "app/views/nota.html"
    });
    $routeProvider.when("/curso", {
        //controller: "cursoController",
        templateUrl: "app/views/curso.html"
    });
    $routeProvider.when("/cuenta", {
        //controller: "cuentaController",
        templateUrl: "app/views/cuenta.html"
    });
    $routeProvider.when("/tutor", {
        //controller: "tutorController",
        templateUrl: "app/views/tutor.html"
    });
    $routeProvider.when("/cuentatutor", {
        templateUrl: "app/views/cuentatutor.html"
    });

    $routeProvider.when("/orders", {
        controller: "ordersController",
        templateUrl: "/app/views/orders.html",
        resolve: {
            checkSecurity: checkSecurity,
        },
        settings: {
            loginRequired: true,
            roles: ['User'],
        }
    });



    $routeProvider.otherwise({ redirectTo: "/home" });

});





checkSecurity.$inject = ['$route', '$q', '$location', 'authService'];
function checkSecurity($route, $q, $location, authService) {
    var deferred = $q.defer();
    authService.fillAuthData().then(function () {
        var settings = $route.current.settings;
        var loginRequired = settings.loginRequired || false;
        var roles = settings.roles || [];
        if (loginRequired) {
            if (!authService.authentication.isAuth) {
                $location.path('/login');
            } else {
                if (roles.length > 0) {
                    if (!checkRole(authService.authentication.roles, roles)) {
                        $location.path('/notauthorized').replace();
                    }
                }
            }
        }
        deferred.resolve(true); //We want to return just true even if we have to re-route. 
        //If we returned an reject, the the global handler will re-route us to home
    }, function (error) {
        deferred.reject(error);
    });

    return deferred.promise;
}

function checkRole(userRoles, rolesToCheck) {
    if (rolesToCheck.length == 0) {
        return true;
    }
    if (userRoles.length == 0) {
        return false;
    }
    for (var i = 0; i < userRoles.length; i++) {
        if (rolesToCheck.indexOf(userRoles[i]) > -1) {
            return true;
        }
    }
    return false;
}

